﻿using MongoDB.Driver;
using SimpleInjector;
using Sustainalytics.CDDB.DataConnector;
using Sustainalytics.Synchronization.Interface;
using BackOfficeAppSettings = Sustainalytics.Entities.BackOfficeAppSettings;
using Sustainalytics.MongoDB.Extensions;
using Sustainalytics.SharedEntities.Corporate;
using System.Collections.Generic;
using System.Configuration.Abstractions;
using System.Linq;
using CompanyProfileResearchState = Sustainalytics.BusinessResearchProductStates.Entities.ResearchState.CompanyProfileResearchState;

namespace Sustainalytics.ClientDW.Synchronization.SyncHbaseData
{
    public static class SimpleInjectorConfig
    {
        public static Container Register(
            ILogger logger,
            Dictionary<string, string> gaConnectionStrings,
            Dictionary<string, string> dsConnectionStrings)
        {
            var container = new Container();

            container.RegisterCollections(gaConnectionStrings);
            container.RegisterMisc(logger);
            container.RegisterAppConfig();

            container.Verify();

            return container;
        }

        private static void RegisterCollections(
            this Container container,
            Dictionary<string, string> gaConnectionStrings)
        {
            container.RegisterClientDWCollections(gaConnectionStrings["ClientDW"]);
            container.RegisterGovernanceDWCollections(gaConnectionStrings["GovernanceDW"]);
        }


        private static void RegisterClientDWCollections(this Container container, string connectionString)
        {
            container.Register(() => MongoFactory.GetCollectionFromDatabase<Entities.CorporateData>(connectionString));
            container.Register(() => MongoFactory.GetCollectionFromDatabase<CompanyProfileResearchState>(connectionString));
            container.Register(() => MongoFactory.GetCollectionFromDatabase<Entities.FinancialData>(connectionString));

            container.Register(() => MongoFactory.GetCollectionFromDatabase<Country>(connectionString));
            container.Register(() => MongoFactory.GetCollectionFromDatabase<Region>(connectionString));
            container.Register(() => MongoFactory.GetCollectionFromDatabase<PeerGroup>(connectionString));
        }

        private static void RegisterGovernanceDWCollections(this Container container, string connectionString)
        {
            container.Register(() => MongoFactory.GetCollectionFromDatabase<GovernanceRegion>(connectionString));
        }

        private static void RegisterMisc(this Container container, ILogger logger)
        {
            container.Register(() => logger, Lifestyle.Singleton);
            container.Register<BackOfficeAppSettings>(Lifestyle.Singleton);
            container.RegisterSingleton(() => new CorporateDataRepository(container.GetInstance<BackOfficeAppSettings>().ThriftServer,
                                                                            container.GetInstance<BackOfficeAppSettings>().ThriftPort,
                                                                            container.GetInstance<BackOfficeAppSettings>().HbaseTablePrefix,
                                                                            container.GetInstance<BackOfficeAppSettings>().HiveConnectionString));
        }

        private static void RegisterAppConfig(this Container container)
        {
            container.RegisterSingleton(() => ConfigurationManager.Instance.AppSettings);
            container.RegisterSingleton(() => ConfigurationManager.Instance.ConnectionStrings);
        }
    }
}
